package com.group.admin.controller;

import com.group.admin.entity.ShopCommodityEntity;
import com.group.admin.service.ShopCommodityService;
import com.group.admin.vo.ShopSku;
import com.group.common.to.SeckillOrderTo;
import com.group.common.utils.PageUtils;
import com.group.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Random;


/**
 * 商品表
 *
 * @author gxx
 * @email hongyezhaofang.com
 * @date 2021-11-13 10:30:09
 */
@RestController
@RequestMapping("admin/shopcommodity")
public class ShopCommodityController {
    @Autowired
    private ShopCommodityService shopCommodityService;
    /*
    * 锁库存
    * */
    @RequestMapping("/lockStock")
    public R lockStock(@RequestBody SeckillOrderTo to){
        shopCommodityService.lockStock(to);
        return R.ok();
    }

    @RequestMapping("/info/{skuId}")
    public R getSkuInfoBySkuId(@PathVariable("skuId") Integer skuId){
        ShopCommodityEntity byId = shopCommodityService.getById(skuId);
        return R.ok().setData(byId);
    }

    /*
    商品管理-新增
     */
    @RequestMapping("/save")
    public R save(@RequestBody ShopCommodityEntity shopCommodityEntity){
        shopCommodityService.save(shopCommodityEntity);

        return R.ok();
    }


    /*
    查询全部商品
     */
    @RequestMapping("/shoplist")
    public R shoplist(@RequestParam Map<String, Object> params){
        //查询全部商品 模糊查询商品名称
        PageUtils pageUtils = shopCommodityService.queryPage(params);
        //查询成功返回结果
        return R.ok("查询成功").put("data",pageUtils);
    }

    /*
    查询全部商品信息和规格参数
     */
    @RequestMapping("/skulist")
    public R skulist(){
        //sql查询商品全部信息
       List<ShopSku> skulist= shopCommodityService.skulist();
       //查询成功返回打印结果
       return R.ok("查询成功").put("data",skulist);
    }

    /*
    商品管理-删除
     */
    @DeleteMapping("/delById/{skuId}")
    public R delById(@PathVariable("id")Integer skuId){
        if (skuId==null){
            return R.error("ID不能为空");
        }
        // 删除之前先查询
        ShopCommodityEntity commodityEntity = shopCommodityService.getById(skuId);
        if (commodityEntity==null){
            return R.ok("用户已经删除了");
        }
        boolean b = shopCommodityService.removeById(skuId);
        if (b){
            return R.ok("删除用户成功");
        }
        return R.error("删除用户失败");
    }

    /*
    商品管理-修改
     */
    @PutMapping("/update")
    public R update(@RequestBody ShopCommodityEntity shopCommodityEntity){
        sleep();
        R r = checkUserBoUpdate(shopCommodityEntity);
        if (!r.isEmpty()){
            return r;
        }
        shopCommodityService.updateById(shopCommodityEntity);

        return R.ok("更新用户成功");
    }

    private void sleep(){
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private R checkUserBoUpdate(ShopCommodityEntity shopCommodityEntity){
        if (shopCommodityEntity.getSkuId()==null){
            return R.error("商品id不能为空");
        }
        if (shopCommodityEntity.getCommodityImg()==null){
            return R.error("商品图片不能为空");
        }
        if (shopCommodityEntity.getCommodityName()==null){
            return R.error("商品名称不能为空");
        }
        if (shopCommodityEntity.getCommodityPrice()==null){
            return R.error("商品售价不能为空");
        }
        if (shopCommodityEntity.getCommodityNum()==null){
            return R.error("商品销量不能为空");
        }
        if (shopCommodityEntity.getCommodityStock()==null){
            return R.error("商品库存不能为空");
        }
        if (shopCommodityEntity.getCommoditySort()==null){
            return R.error("商品排序不能为空");
        }
        if (shopCommodityEntity.getCommodityStatus()==null){
            return R.error("商品状态不能为空");
        }
        if (shopCommodityEntity.getStockLock()==null){
            return R.error("锁库存不能为空");
        }

        return R.ok();
    }
}
